package com.example.jd.finance.web;


import com.example.jd.common.InvokeResultUtil;
import com.example.jd.common.enums.BizStatusCodeEnum;
import com.example.jd.finance.entity.MsWechatIncreaseFriends;
import com.example.jd.finance.entity.param.DetailPageParam;
import com.example.jd.finance.entity.param.MsQueryEnterpriseWechatCodePageParam;
import com.example.jd.finance.entity.vo.MsQueryDetailCountVO;
import com.example.jd.finance.sever.MsCustomerRecordsService;
import com.example.jd.finance.sever.MsWechatIncreaseCustomerDetailsService;
import com.example.jd.finance.sever.MsWechatIncreaseFriendsService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-11-06
 */
@RestController
@Slf4j
@RequestMapping("/details")
public class MsWechatIncreaseCustomerDetailsController {

    @Autowired
    private MsCustomerRecordsService msCustomerRecordsService;

    @Autowired
    private MsWechatIncreaseCustomerDetailsService msWechatIncreaseCustomerDetailsService;

    @Autowired
    private MsWechatIncreaseFriendsService msWechatIncreaseFriendsService;


    /**
     * 申请好友统计页面-分页查询
     */
    @PostMapping("/enterprisePage")
    public InvokeResultUtil<Object> queryEnterpriseWechatCodePage(@RequestBody MsQueryEnterpriseWechatCodePageParam param){

        try {
            log.info("MsWechatIncreaseCustomerDetailsController-->queryEnterpriseWechatCodePage入参为：{}", param);

            return msWechatIncreaseCustomerDetailsService.queryEnterpriseWechatCodePage(param);
        } catch (Exception e) {
            log.error("MsWechatIncreaseCustomerDetailsController--->查询接口queryEnterpriseWechatCodePage异常：", e);

            return InvokeResultUtil.failure(BizStatusCodeEnum.ERROR_SYSTEM.getCode(), "查询异常");
        }
    }

    /**
     * 申请好友统计页面 查询 excel
     */
    @GetMapping("enterprisePageExcel")
    @ApiOperation(value = "申请好友统计页面excel", httpMethod = "GET")
    public InvokeResultUtil enterprisePageExcel(MsQueryEnterpriseWechatCodePageParam param, HttpServletResponse response) {
        if (param == null){
            return InvokeResultUtil.failure(BizStatusCodeEnum.MISSING_PARAMS.getCode(),"缺少参数");
        }
        try {
            return InvokeResultUtil.success(msWechatIncreaseCustomerDetailsService.enterprisePageExcel(param, response));
        } catch (Exception ex) {
            log.error("enterprisePageExcel操作失败 {}" , ex);
            return InvokeResultUtil.failure(BizStatusCodeEnum.ERROR_SYSTEM.getCode(), "操作异常");
        }
    }

    /**
     * 申请好友统计页面-统计当日 累计等
     */
    @PostMapping("/enterpriseStatistics")
    public InvokeResultUtil<Object> queryEnterpriseWechatCodeStatistics(){

        try {
            return InvokeResultUtil.success(msWechatIncreaseCustomerDetailsService.queryEnterpriseWechatCodeStatistics());
        } catch (Exception e) {
            log.error("MsWechatIncreaseCustomerDetailsController--->查询接口queryEnterpriseWechatCodeStatistics异常：", e);

            return InvokeResultUtil.failure(BizStatusCodeEnum.ERROR_SYSTEM.getCode(), "查询异常");
        }
    }


    /**
     * 企微客户详情统计 当日 累计等
     */
    @PostMapping("/detailCount")
    public InvokeResultUtil<Object> queryDetailCount(@RequestParam String enterpriseWechatCode){

        try {
            if (Objects.isNull(enterpriseWechatCode)){
                return InvokeResultUtil.failure(BizStatusCodeEnum.MISSING_PARAMS.getCode(), "请求参数为空");
            }
            log.info("MsWechatIncreaseCustomerDetailsController-->queryDetailCount入参为：{}", enterpriseWechatCode);
            LocalDate now = LocalDate.now();
            return InvokeResultUtil.success(msCustomerRecordsService.queryDetailCount(enterpriseWechatCode,now));
        } catch (Exception e) {
            log.error("MsWechatIncreaseCustomerDetailsController--->查询接口queryDetailCount异常：", e);

            return InvokeResultUtil.failure(BizStatusCodeEnum.ERROR_SYSTEM.getCode(), "查询异常");
        }

    }

    /**
     * 企微客户详情 分页查询
     */
    @PostMapping("/detailPage")
    public InvokeResultUtil detailPage(@RequestBody DetailPageParam param){
        try {
            log.info("MsWechatIncreaseCustomerDetailsController-->detailPage入参为：{}", param);

            return msCustomerRecordsService.detailPage(param);
        } catch (Exception e) {
            log.error("MsWechatIncreaseCustomerDetailsController--->查询接口detailPage异常：", e);

            return InvokeResultUtil.failure(BizStatusCodeEnum.ERROR_SYSTEM.getCode(), "查询异常");
        }
    }

    /**
     * 企微客户详情 分页查询 excel
     */
    @GetMapping("detailPageExcel")
    @ApiOperation(value = "申请好友统计页面excel", httpMethod = "GET")
    public InvokeResultUtil detailPageExcel(DetailPageParam param, HttpServletResponse response) {
        if (param == null){
            return InvokeResultUtil.failure(BizStatusCodeEnum.MISSING_PARAMS.getCode(),"缺少参数");
        }
        try {
            return InvokeResultUtil.success(msCustomerRecordsService.detailPageExcel(param, response));
        } catch (Exception ex) {
            log.error("enterprisePageExcel操作失败 {}" , ex);
            return InvokeResultUtil.failure(BizStatusCodeEnum.ERROR_SYSTEM.getCode(), "操作异常");
        }
    }

}

